package com.hulk.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hulk.dao.UserInfoMapper;
import com.hulk.model.UserInfo;
import com.hulk.service.UserService;

/**
 * 创建时间：2015-1-27 下午5:22:59
 * 
 * @author andy
 * @version 2.2
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserInfoMapper userInfoMapper;

	@Cacheable(value="userCache", key="#id")
	public UserInfo getUserById(int id) {
		return userInfoMapper.selectByPrimaryKey(id);
	}

	
	public List<Map<String,Object>> getUsers() {
		return userInfoMapper.selectAll();
	}
	
	
	public List<UserInfo> selectAll2() {
		return userInfoMapper.selectAll2();
	}
	
	
	@Transactional(rollbackFor = Exception.class)
	public int insertUserInfo(UserInfo userInfo) throws Exception {
		
		int result = userInfoMapper.insertUserInfo(userInfo);
		if(true){
//			throw new Exception("主动异常");
		}
		System.out.println(result);
//		异步调用.........
		AsyncPush push = new AsyncPush(userInfo);
//		CalloutServices.executor(push);
		return result;
	}
	/**
	 * 异步调用DEMO
	 * @author CUIJIAJUN
	 *
	 */
	class AsyncPush implements Runnable {
		 private UserInfo userInfo ;
		 
		public AsyncPush(UserInfo userInfo){
				this.userInfo = userInfo;
		}
		
		@Override
		public void run() {
			try {
				for(int i=0;i<100;i++){
					System.out.println("这里是异步调用"+i);
				}
			} catch (Exception e) {
//				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}
	}
}
